/**
 * 任务提醒函数
 * @param {Object} opt	el:插件容器Id	,autoDisappear:点击后是否自动消失，
 * 		dragAable：是否可拖拽,tipAudio:是否提示音,tipSrc：提示音的src
 */
function Task(opt){
	this.opt = opt;
	this.init();
	this.addListener();
};
/**
 * 初始化基本结构
 */
Task.prototype.init = function(){
	this.con = document.getElementById(this.opt.el);
	var taskCon = document.createElement('div');
	var taskList = document.createElement('ul');
	taskCon.className = 'task-content';
	taskCon.style.setProperty('display','none');
	taskCon.appendChild(taskList);
	taskList.id = 'task-list';
	var arrowBotton = document.createElement('div');//小箭头
	arrowBotton.className = 'arrow-bottom';
	taskCon.appendChild(arrowBotton);
	this.taskCon = taskCon;//提醒框容器
	this.taskList = taskList;//信息ul
	this.con.appendChild(taskCon);
	this.control = document.getElementById('task-ct');//控制器
	this.infoNum = 0;//信息数量
	this.status = 0;//提醒框状态,0：不显示，1:显示
	this.taskObj = {};//任务对象
	if(this.opt.tipAudio && this.opt.tipSrc){	//增加提示声
		this.aTip = true;	//是否提示的标识
		this.tipPaly = document.createElement('audio');
		this.tipPaly.src = this.opt.tipSrc;
	};
};
/**
 * 设置监听事件
 */
Task.prototype.addListener = function(){
	var _this = this;
	const Event = {	//事件对象
		ctEvent:function(ev){	//控制器点击事件
			console.log('control click');
			if((_this.status == 0) && (_this.infoNum == 0)){
				_this.taskList.innerHTML = '<li><span class="task-name">无任务</span></li>';
				_this.showList();
			}else if(_this.status == 0){
				_this.showList();
			}else if(_this.status == 1){
				_this.hideList();
			};
		},
		itemEvent:function(ev){	//列表点击
			ev.stopPropagation();
			if(ev.target.className =='task-list'){
				try{
					var flag = ev.target.dataset['flag'];
					var cObj = _this.taskObj[flag];
					if(cObj){
						 cObj.cb&&cObj.cb();
					};
					if((_this.opt.autoDisappear) && (cObj.type == 1)){
						_this.removeTask({flag:flag});
					}else if(cObj.type == 2){
//						console.log('点击了type=2的项');
					};
				}catch(e){
					//TODO handle the exception
					console.error('点击无效item');
				};
				
			};
		},
		dragStartEvent:function(ev){	//拖拽开始函数
			dragObj.draging = true;
			dragObj.elRight = Number.parseInt(window.getComputedStyle(_this.con,null).right);
			dragObj.elBottom = Number.parseInt(window.getComputedStyle(_this.con,null).bottom);
//			console.log(dragObj);
			dragObj.startX = ev.clientX;//记录鼠标按下X的坐标
			dragObj.startY = ev.clientY;//记录鼠标按下Y的坐标
			dragObj.elShiftX = dragObj.elRight - ev.clientX;//x偏移量
			dragObj.elShiftY = dragObj.elBottom - ev.clientY;//y偏移量
//			console.log(ev);
			ev.preventDefault();
			ev.stopPropagation();
		},
		dragMoveEvent:function(ev){		//拖拽移动函数
			if(dragObj.draging === true){
				ev.preventDefault();
				ev.stopPropagation();
				var rRight = dragObj.elRight - (ev.clientX - dragObj.startX);
				var rBottom = dragObj.elBottom - (ev.clientY - dragObj.startY);
				_this.con.style.right = rRight + 'px';
				_this.con.style.bottom = rBottom + 'px';
			};
		},
		dragEndEvent:function(ev){	//拖拽结束函数
			if(dragObj.draging === true){
				ev.preventDefault();
				ev.stopPropagation();
				var rRight = dragObj.elRight - (ev.clientX - dragObj.startX);
				var rBottom = dragObj.elBottom - (ev.clientY - dragObj.startY);
				_this.con.style.right = rRight + 'px';
				_this.con.style.bottom = rBottom + 'px';
				dragObj.draging = false;
				var absX = Math.abs(ev.clientX - dragObj.startX);
				var absY = Math.abs(ev.clientY - dragObj.startY);
				if((absX + absY) <= 5){	//判断为点击
					if(_this.status == 1){
						_this.hideList();
					}else if(_this.status == 0){
						_this.showList();
					};
				};
			}
		}
	};
	var dragObj = {//拖动相关参数
		draging:false,	//鼠标是否按下
		elShiftX:0,	//容器相对于鼠标按下时x的偏移量
		elShiftY:0,	//容器相对于鼠标按下时y的偏移量
	};
	this.control.addEventListener('mousedown',Event.ctEvent,false);
	this.taskList.addEventListener('click',Event.itemEvent,false);
	if(this.opt.dragAable){	//配置为可拖拽
		this.control.addEventListener('mousedown',Event.dragStartEvent,true);
		this.control.addEventListener('mousemove',Event.dragMoveEvent,true);
		this.control.addEventListener('mouseup',Event.dragEndEvent,true);
	};
};
/**
 * 展示列表
 * @param {Object} opt
 */
Task.prototype.hideList = function(opt){
	if(this.status == 1){
		this.taskCon.style.setProperty('display','none');
		this.control.innerHTML = '<i class="taskfont">&#xe651;</i>';
		this.status = 0;
	};
};
/**
 * 隐藏列表
 * @param {Object} opt
 */
Task.prototype.showList = function(opt){
	if(this.status == 0){
		this.taskCon.style.setProperty('display','block');
		this.control.innerHTML = '<i class="taskfont">&#xe616;</i>';
		this.status = 1;
	};
};
/**
 * 添加任务
 * @param {Object} opt	title:标题,num:数量,cb:点击的回调函数 ,flag:本条信息唯一表示,type:1(点击消失的类型)，2(点击存在的类型)
 */
Task.prototype.addTask = function(opt){
	if(this.taskObj.hasOwnProperty(opt.flag)){	//更新通知
		var updateObj = this.taskObj[opt.flag];
		if(updateObj.num != opt.num){	//数量有变化就会更新
			this.aTip && this.tipPaly.play();
			updateObj.li.innerHTML = '<span class="task-name">'+ opt.title +'</span><span class="task-num">'+ opt.num +'</span>';
			this.showList();
		};
	}else{	//新增通知
		if(opt.num != 0){
			this.aTip && this.tipPaly.play();
		};
		var tLi = document.createElement('li');
		tLi.dataset.flag = opt.flag;
		tLi.className = 'task-list';
		tLi.innerHTML = '<span class="task-name">'+ opt.title +'</span><span class="task-num">'+ opt.num +'</span>';
		var tObj = {
			li:tLi,
			cb:opt.cb,
			num:opt.num,
			type:opt.type
		};
		this.taskObj[opt.flag] = tObj;
		if(this.infoNum == 0){
			this.taskList.innerHTML = '';
		};
		this.taskList.appendChild(tLi);
		this.infoNum ++;
		this.showList();
	};
	
	
};
/**
 * 删除任务
 * @param {Object} opt	--flag：要删除的标志位
 */
Task.prototype.removeTask = function(opt){
	if(this.taskObj.hasOwnProperty(opt.flag)){
		var rObj = this.taskObj[opt.flag];
		this.taskList.removeChild(rObj.li);
		rObj = null;
		delete this.taskObj[opt.flag];
		this.infoNum--;
		if(this.infoNum == 0){
			this.hideList();
		};
	};
};


